const config = require('../../../config/config.local');
const amazonMws = require('amazon-mws')(config.AWSAccessKeyId, config.AWSSecretKey);
const isProduction = process.env.NODE_ENV === 'production';

const listInboundShipments = {};
listInboundShipments.generateOption = function(seller_id, mws_auth_token, shipmentId, shipmentStatusListMemberList=null, shipmentIdListMemberList=null, lastUpdatedAfter=null, lastUpdatedBefore=null){
  let baseOption = {
    'Version': '2010-10-01',
    'Action': 'ListInboundShipments',
    'SellerId': seller_id,
    'MWSAuthToken': mws_auth_token,
    'ShipmentId': shipmentId,
  };

  if (lastUpdatedAfter)
    baseOption['LastUpdatedAfter'] = lastUpdatedAfter;
  if (lastUpdatedBefore)
    baseOption['LastUpdatedBefore'] = lastUpdatedBefore;

  if (shipmentStatusListMemberList) {
    let keyPrefix = "ShipmentStatusList.member";
    shipmentStatusListMemberList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item;
    });
  }
  if (shipmentIdListMemberList) {
    let keyPrefix = "ShipmentIdList.member";
    shipmentIdListMemberList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item;
    });
  }
  return baseOption;
};

listInboundShipments._ = function(options) {
  if (!isProduction) {
    return amazonMws.fulfillmentInboundShipment.search(options);
  } else {
    console.error("bot not implemented!")
  }
};

listInboundShipments.listInboundShipments = function(seller_id, mws_auth_token, shipmentId, shipmentStatusListMemberList=null, shipmentIdListMemberList=null, lastUpdatedAfter=null, lastUpdatedBefore=null) {
  return this._(this.generateOption(seller_id, mws_auth_token, shipmentId, shipmentStatusListMemberList, shipmentIdListMemberList, lastUpdatedAfter, lastUpdatedBefore));
};

module.exports = listInboundShipments;